import { SELECT_MODE, SELECT_STYLE, Selector } from 'babylon-lib/pick/selector/selector';
import { DefaultStage } from '../default-stage';
import { AbstractCommunity } from '../community/abstract-community';
import { Troop } from '../attachment/troop';
import { CommunitySelectConfig } from './community-select-config';
import { TroopSelectConfig } from './troop-select-config';


class SelectManager {

    private static _selector: Selector;
    private static _stage: DefaultStage;
    private static _communityConfig = new CommunitySelectConfig();
    private static _troopConfig = new TroopSelectConfig();

    public static initialize(stage: DefaultStage): void {
        this._stage = stage;
        this._selector = new Selector(stage.mainScene, {
            mode: SELECT_MODE.SINGLE,
            style: SELECT_STYLE.CLICK_PICK,
            clearOnEmpty: true,
            unselectOnRepeat: false,
            doNotClearScenePickable: true,
            clickDitherThreshold: 1,
        });

        const scope = this;
        this._selector.onSelect((targets: Array<AbstractCommunity | Troop>) => {
            targets.forEach(target => scope.selectHandle(target));
        });

        this._selector.onUnselect((targets: Array<AbstractCommunity | Troop>) => {
            targets.forEach(target => scope.unselectHandle(target));
        });
    }

    public static select(target: AbstractCommunity | Troop): void {
        this._selector.select(target);
    }

    public static unselect(target: AbstractCommunity | Troop): void {
        this._selector.unselect(target);
    }

    public static addCommunityTarget(communities: AbstractCommunity[]): void {
        communities.forEach(community => {
            this._selector.addTarget(community, this._communityConfig);
        });
    }

    public static addTroopTarget(troops: Troop[]): void {
        troops.forEach(troop => {
            this._selector.addTarget(troop, this._troopConfig);
        });
    }

    private static selectHandle(target: AbstractCommunity | Troop): void {
        console.log('selectHandle', target);
    }

    private static unselectHandle(target: AbstractCommunity | Troop): void {
        console.log('unselectHandle', target);
    }

}


export { SelectManager };